/********************************************************************************
* Description: execute event loop in another thread
* Author: hsqiang
* Create Date: 2023-07-09
********************************************************************************/
#ifndef EVENT_WORKER_HPP_
#define EVENT_WORKER_HPP_

#include "event_loop.hpp"
#include <thread>
#include <future>

namespace event_framework
{
    class EventWorker : Noncopyable
    {
    public:
        EventWorker() = default;
        ~EventWorker() { ASSERT(worker.joinable()); worker.join(); }
        EventLoop& GetEventLoop() { return loop; }
        void Start()
        {
            worker = std::thread(&EventWorker::Run, this);
            pro.get_future().get();
        }
        void Stop() { loop.Break(); }
    private:
        void Run()
        {
            pro.set_value();
            loop.Loop();
        }
        EventLoop loop;
        std::thread worker;
        std::promise<void> pro;
    };
}

#endif